/**
 *  @file : globals.js
 *  @brief: 全局模块
 */
define(['jquery'], function ($) {
  var _channel = $({}); // 关注者模式
  return $.extend({
    subscribe: function () {
      _channel.bind.apply(_channel, arguments);
    },
    unsubscribe: function () {
      _channel.unbind.apply(_channel, arguments);
    },
    publish: function () {
      _channel.trigger.apply(_channel, arguments);
    },
    time: function () {
      return Math.floor(new Date().getTime() / 1000)
    },
    a2s: function (ab, trim) { // ArrayBuffer => string
      var c = new Uint16Array(ab);
      var a = String.fromCharCode.apply(null, c);
      if (trim) {
        a = a.replace(/[\s\x00]+$/, '');
      }
      return a;
    },
    s2a: function (str, max) { // string => ArrayBuffer
      var buf = new Uint16Array(max);
      for (var i = 0; i < max; i++) {
        buf[i] = str.charCodeAt(i);
      }
      return buf.buffer;
    }
  });
});
